package com.youbug.mall.coupon_sms.feign.impl;

import com.youbug.common.to.MemberPrice;
import com.youbug.common.to.SkuReductionTo;
import com.youbug.common.to.SpuBoundTo;
import com.youbug.mall.product_pms.feign.ICouponFeignService;
import com.youbug.mall.coupon_sms.entity.SmsMemberPrice;
import com.youbug.mall.coupon_sms.entity.SmsSkuFullReduction;
import com.youbug.mall.coupon_sms.entity.SmsSkuLadder;
import com.youbug.mall.coupon_sms.entity.SmsSpuBounds;
import com.youbug.mall.coupon_sms.service.ISmsMemberPriceService;
import com.youbug.mall.coupon_sms.service.ISmsSkuFullReductionService;
import com.youbug.mall.coupon_sms.service.ISmsSkuLadderService;
import com.youbug.mall.coupon_sms.service.ISmsSpuBoundsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CouponFeignServiceImpl implements ICouponFeignService {

    @Autowired
    private ISmsSkuLadderService skuLadderService;

    @Autowired
    private ISmsSkuFullReductionService skuFullReductionService;

    @Autowired
    private ISmsMemberPriceService memberPriceService;

    @Autowired
    private ISmsSpuBoundsService spuBoundsService;

    @Override
    @Transactional(transactionManager = "transactionManagerSms", rollbackFor = Exception.class)
    public void saveSpuBounds(SpuBoundTo spuBoundTo) {
        SmsSpuBounds bounds = new SmsSpuBounds();
        BeanUtils.copyProperties(spuBoundTo, bounds);
        spuBoundsService.save(bounds);
    }

    @Override
    @Transactional(transactionManager = "transactionManagerSms", rollbackFor = Exception.class)
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        SmsSkuLadder skuLadder = new SmsSkuLadder();
        skuLadder.setSkuId(reductionTo.getSkuId());
        skuLadder.setFullCount(reductionTo.getFullCount());
        skuLadder.setDiscount(reductionTo.getDiscount());
        skuLadder.setAddOther(reductionTo.getCountStatus());
        if(reductionTo.getFullCount() > 0){
            skuLadderService.save(skuLadder);
        }

        SmsSkuFullReduction fullReduction = new SmsSkuFullReduction();
        BeanUtils.copyProperties(reductionTo, fullReduction);
        if(fullReduction.getFullPrice().compareTo(new BigDecimal("0"))==1){
            skuFullReductionService.save(fullReduction);
        }

        List<MemberPrice> memberPrices = reductionTo.getMemberPrice();

        List<SmsMemberPrice> collect = memberPrices.stream().map(m -> {
            SmsMemberPrice memberPrice = new SmsMemberPrice();
            memberPrice.setSkuId(reductionTo.getSkuId());
            memberPrice.setMemberLevelId(m.getId());
            memberPrice.setMemberLevelName(m.getName());
            memberPrice.setMemberPrice(m.getPrice());
            memberPrice.setAddOther(1);
            return memberPrice;
        }).filter(e->e.getMemberPrice().compareTo(new BigDecimal("0"))==1).collect(Collectors.toList());

        memberPriceService.saveBatch(collect);
    }
}
